package com.td.erhome.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.td.erhome.R;
import com.td.erhome.base.BaseFragment;
import com.td.erhome.ui.login.LoginActivity;
import com.td.erhome.ui.min.AboutusActivity;
import com.td.erhome.ui.min.InviteFriendActivity;
import com.td.erhome.ui.min.MyCollectActivity;
import com.td.erhome.ui.min.MyCouponActivity;
import com.td.erhome.ui.min.MyMessageActivity;
import com.td.erhome.ui.sellhouse.MyPublishActivity;
import com.td.erhome.utils.ConstUtils;
import com.td.erhome.utils.ImageLoader;
import com.td.erhome.utils.RxBus;
import com.td.erhome.utils.UserManager;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;


public class MinFragment extends BaseFragment {

    @Bind(R.id.img_head)
    ImageView mImgHead;
    @Bind(R.id.name)
    TextView mName;
    @Bind(R.id.tv_wdl)
    TextView mTvWdl;
    @Bind(R.id.re_min_sc)
    RelativeLayout mReMinSc;
    @Bind(R.id.re_nin_see_notes)
    RelativeLayout mReNinSeeNotes;
    @Bind(R.id.re_min_sell_notes)
    RelativeLayout mReMinSellNotes;
    @Bind(R.id.re_min_coupon)
    RelativeLayout mReMinCoupon;
    @Bind(R.id.re_recomment_friend)
    RelativeLayout mReRecommentFriend;
    @Bind(R.id.re_min_us)
    RelativeLayout mReMinUs;
    @Bind(R.id.re_min_call_us)
    RelativeLayout mReMinCallUs;
    @Bind(R.id.re_min_gl)
    RelativeLayout mReMinGl;
    private Intent mIntent;
    private Subscription mSubscribe;

    public static MinFragment newInstance() {
        Bundle bundle = new Bundle();
        MinFragment minFragment = new MinFragment();
        minFragment.setArguments(bundle);
        return minFragment;
    }

    @Override
    protected void init() {

        mSubscribe = RxBus.getInstance().toObserverable(Integer.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer i) {
                        if (i== ConstUtils.LOGOUT)  ChangeUserInfo();
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_min;
    }

    @Override
    protected void initView(View view) {
        mIntent = new Intent(getActivity(), MyCollectActivity.class);
        ChangeUserInfo();
    }

    private void ChangeUserInfo() {
        if (UserManager.getIns().getUser()!=null){
            ImageLoader.loadCicleImage(this, UserManager.getIns().getUser().head_img, R.mipmap.td_min_header,mImgHead);
            mName.setVisibility(View.VISIBLE);
            mTvWdl.setVisibility(View.GONE);
            mName.setText(UserManager.getIns().getUser().mobile);
        }else {
            ImageLoader.loadCicleImage(this, "",R.mipmap.td_min_header,mImgHead);
            mName.setVisibility(View.GONE);
            mTvWdl.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initOperation() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (!mSubscribe.isUnsubscribed()){
            mSubscribe.unsubscribe();
        }
    }

    @OnClick({R.id.img_head, R.id.name, R.id.tv_wdl, R.id.re_min_sc,
            R.id.re_nin_see_notes, R.id.re_min_sell_notes, R.id.re_min_coupon,
            R.id.re_recomment_friend, R.id.re_min_us, R.id.re_min_call_us,R.id.re_min_gl})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_head:
                if (UserManager.getIns().getUser()==null){
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }else {
                    Intent intent = new Intent(getActivity(),MyMessageActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.name:
                break;
            case R.id.tv_wdl:
                break;
            case R.id.re_min_sc:
                if (UserManager.getIns().getUser()==null){
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }

                mIntent.putExtra("type","我的收藏");
                startActivity(mIntent);
                break;
            case R.id.re_nin_see_notes:
                if (UserManager.getIns().getUser()==null){
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                mIntent.putExtra("type","我的浏览记录");
                startActivity(mIntent);
                break;
            case R.id.re_min_sell_notes:
                if (UserManager.getIns().getUser()==null){
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                mIntent.putExtra("type","看房记录");
                startActivity(mIntent);
                break;
            case R.id.re_min_coupon: //优惠券
                if (UserManager.getIns().getUser()==null){
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                Intent intent1 = new Intent(getActivity(), MyCouponActivity.class);
                startActivity(intent1);
                break;
            case R.id.re_recomment_friend:

                if (UserManager.getIns().getUser()==null){
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                Intent intent2 = new Intent(getActivity(),InviteFriendActivity.class);
                startActivity(intent2);
                break;
            case R.id.re_min_us:
                Intent intent3 = new Intent(getActivity(), AboutusActivity.class);
                startActivity(intent3);
                break;
            case R.id.re_min_call_us:
                break;
            case R.id.re_min_gl:
                if (UserManager.getIns().getUser()==null){
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                }
                Intent intent = new Intent(getActivity(),MyPublishActivity.class);
                startActivity(intent);
                break;
        }
    }
}
